package com.leyou.user.controller;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.pojo.PageResult;
import com.leyou.common.utils.CookieUtils;
import com.leyou.user.config.JwtProperties;
import com.leyou.user.pojo.Address;
import com.leyou.user.pojo.Collect;
import com.leyou.user.pojo.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;


    /**
     * @param data 要校验的数据
     * @param type 要校验的数据类型：1，用户名；2，手机；
     * @return
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkDate(@PathVariable("data") String data, @PathVariable("type") Integer type) {
        try {
            Boolean boo = this.userService.checkDate(data, type);

            if (boo == null) {
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
            return ResponseEntity.ok(boo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }



    /**
     * 发送手机验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("/send")
    public ResponseEntity<Void> sendVerifyCode(String phone) {
        try {
            Boolean boo = this.userService.sendVerifyCode(phone);

            if (boo == null || !boo) {
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }

            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据用户名和密码查询用户
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<User> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        try {
            User user = userService.queryUserByUserName(username, password);

            if (user == null) {
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }

            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 注册
     *
     * @param user
     * @param code
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code) {
        Boolean boo = this.userService.register(user, code);
        if (boo == null || !boo) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 查询tb_collections中的所有的商品
     * @param page
     * @param rows
     * @param enable
     * @return
     */
    @GetMapping("collections")
    public ResponseEntity<PageResult<Collect>> findAllSkusBySkuId(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "8") Integer rows,
            @RequestParam(value = "enable", required = false) Boolean enable
    ) {

        PageResult<Collect> collectList = userService.findAllSkusBySkuId(page,rows,enable);

        if(CollectionUtils.isEmpty(collectList.getItems())){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(collectList);
    }

    /**
     * 重新添加至购物车
     * @param collect
     * @return
     */
    @PostMapping("collections/add")
    public ResponseEntity<Void> deleteCollectionById(Collect collect){
        Boolean b = userService.deleteCollectionById(collect);
        if(b==null||b==false){
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return ResponseEntity.ok().build();
    }

}
